<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\helpers\v1\traits\SearchDataForModel;
use app\api\model\v1\AdminLog;
use app\api\model\v1\Documents;
use app\api\model\v1\HelpCenter;
use app\api\model\v1\Notice;
use app\BaseController;
use think\facade\Env;
use think\Request;

class System extends BaseController
{
    use SearchDataForModel;

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/28 16:17
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 通知list
     */
    public function getNoticeList(){
        $limit = $this->request->param('limit',10);
        $res = $this->search(new Notice(),[],$limit,'desc');
        return $this->response($res);
    }
    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/28 16:16
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 保存通知
     */
    public function saveNotice(){
        $id = $this->request->param('id', 0);
        $data = $this->request->param();
        $data['uid'] = $this->request->uid; //用户id
        if (intval($id) === 0) {
            Notice::create($data);
        } else {
            $res = Notice::find($id);
            if (is_null($res)) {
                return $this->response(403, '数据不存在');
            }
            $res->save($data);
        }
        AdminLog::record(intval($id) === 0 ? '新增通知' : '编辑通知', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }


    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/28 16:23
     * @param $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 删除通知
     */
    public function deleteNotice($id)
    {
        $res = Notice::find($id);
        if (is_null($res)) {
            return $this->response(403, '找不到该资源信息');
        }
        $res->force()->delete();
        AdminLog::record('删除通知', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }


    /* +++++++++++++++++ 问题中心 ++++++++++++++++++ */

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/14 9:42
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 数据源读取
     */
    public function getHelpCenterList(){
        $limit = $this->request->param('limit', 10);
        $res = HelpCenter::order(['create_time' => 'desc'])->paginate($limit);
        return $this->response($res);
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/14 9:41
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 新增and编辑问题
     */
    public function saveHelpCenter(){
        $id = $this->request->param('id', 0);
        $data = $this->request->param();
        $data['admin_id'] = $this->request->uid; //用户id
        if (intval($id) === 0) {
            HelpCenter::create($data);
        } else {
            $res = HelpCenter::find($id);
            if (is_null($res)) {
                return $this->response(403, '数据不存在');
            }
            $res->save($data);
        }
        AdminLog::record(intval($id) === 0 ? '新增问题' : '编辑问题', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/14 9:52
     * @param $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 删除
     */
    public function deleteHelpCenter($id)
    {
        $res = HelpCenter::find($id);
        if (is_null($res)) {
            return $this->response(403, '找不到该资源信息');
        }
        //->force()
        $res->delete();
        AdminLog::record('删除问题', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }

    /* ++++++++++++++++++ 制度文件 +++++++++++++++++++ */
    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/14 9:42
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 数据源读取
     */
    public function getDocumentsList(){
        $limit = $this->request->param('limit', 10);
        $res = Documents::order(['create_time' => 'desc'])->paginate($limit)->each(function ($item, $key) {
            $item['url'] = Env::get('IMG_HTTP.img_http').$item['url'];
            return $item;
        });;
        return $this->response($res);
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/14 9:41
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 新增and编辑问题
     */
    public function saveDocuments(){
        $id = $this->request->param('id', 0);
        $data = $this->request->param();
        $data['admin_id'] = $this->request->uid; //用户id
        if (intval($id) === 0) {
            Documents::create($data);
        } else {
            $res = Documents::find($id);
            if (is_null($res)) {
                return $this->response(403, '数据不存在');
            }
            $res->save($data);
        }
        AdminLog::record(intval($id) === 0 ? '新增问题' : '编辑问题', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/1/14 9:52
     * @param $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 删除
     */
    public function deleteDocuments($id)
    {
        $res = Documents::find($id);
        if (is_null($res)) {
            return $this->response(403, '找不到该资源信息');
        }
        //->force()
        $res->delete();
        AdminLog::record('删除问题', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }

}
